package com.esms.controller;

import com.esms.po.RankBonus;
import com.esms.po.WorkingYearsBonus;
import com.esms.service.IRankBonusService;
import com.esms.service.IWorkYearService;
import com.esms.utils.Message;
import com.esms.vo.WokingYearsBonusPages;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lyy
 * Date: 2021-09-30
 * Time: 21:59
 * Projectname:   ssm
 */
@Controller
public class WorkYearController {
    @Autowired
    private IWorkYearService service;

    /*查询所有数据*/
    @RequestMapping("/findWorkingYearBonusList.do")
    @ResponseBody
    public WokingYearsBonusPages findAll(
            @RequestParam(value="page", defaultValue="1")int pageNum,
            @RequestParam(value="limit", defaultValue="5") int limit,
            @RequestParam(value="year", defaultValue="0") int year) throws Exception {
        List<WorkingYearsBonus> list;
        //模糊查询，有多少个条件就接收多少个字段
        WorkingYearsBonus wyb = new WorkingYearsBonus();
        wyb.setWybYear(year);
        //pageNum:起始页面  pageSize:每页的大小
        PageHelper.startPage(pageNum,limit);
        //查找条件，一定要紧跟在startPage后
        list = service.findSelective(wyb);
        PageInfo pageResult = new PageInfo(list);
        //设置前台需要的数据
        WokingYearsBonusPages wybp = new WokingYearsBonusPages();
        wybp.setCode(0);
        wybp.setMsg("");
        wybp.setCount((int) pageResult.getTotal());
        wybp.setData(pageResult.getList());
        return wybp;
    }

    /**
     * 添加工龄奖金操作
     * @return
     */
    @RequestMapping("/addWorkingYearsBonus.do")
    @ResponseBody
    public Message addWorkingYearsBonus(WorkingYearsBonus workingYearsBonus){
        if(service.Count(workingYearsBonus.getWybYear())>0)
        {
           return Message.error("工龄已存在");
        }
        service.addWorkYearBonus(workingYearsBonus);
        return Message.success("添加工龄成功");
    }
   
    //根据id删除数据
    @RequestMapping("/deleteRecord.do")
    @ResponseBody
    public Message doDelete(int id){
       try {
    	   service.moveWorkYearBonus(id);
       }catch (Exception e) {
		// TODO: handle exception
    	   return Message.error("删除失败,有关联数据");
	}
       	return Message.success("删除成功");
    }
    /**
     * 更新工龄操作
     * @return
     */
    @RequestMapping("/updateRecord.do")
    @ResponseBody
    public Message updateRecord(WorkingYearsBonus workingYearsBonus){
    	Integer wybYear = workingYearsBonus.getWybYear();
        WorkingYearsBonus findByYear = service.findByYear(wybYear);
        if(findByYear!=null&&!findByYear.getWybId().equals(workingYearsBonus.getWybId()))
        {
            return Message.error("工龄已存在");
        }
        
        if(!service.modifyWorkYearBonus(workingYearsBonus)) {
        	return Message.error("工龄更新失败");
        }
     return Message.success("工龄更新成功");
        
    }
}
